/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2015-01-27 11:16:59
 * @filename : iscroll.js
 * @version : 
 * @description : touch.pull是解决下拉后回复，scrollTop会增加的BUG。 使用TOP，滚动条会跟随下拉。CSS3不会。
 */

;(function($){
   
    var touch = {move:0};

    var pull = {

        init : function(opt) {
            this.opt = opt;
            this.bindEvt(opt);
        },

        bindEvt : function(opt) {                        
            var tthis = this;

            opt.dom.find('ul').on('touchstart',function(e){
                touch.x1 = e.touches[0].pageX;
                touch.y1 = e.touches[0].pageY;
                touch.x2 = e.touches[0].pageX;
                touch.y2 = e.touches[0].pageY;
                touch.targetTop = e.target.offsetTop;                
                touch.bool = true; 
                console.dir(e.target);
            }).on('touchmove',function(e){
                if (touch.bool) {
                    if (touch.pull) {
                        e.preventDefault();
                        e.stopPropagation(); 
                    }
                    touch.x2 = e.touches[0].pageX;
                    touch.y2 = e.touches[0].pageY;
                    touch.direction = tthis.touchDirection(touch.x1,touch.x2,touch.y1,touch.y2);
                    if (touch.direction == 'Up' || touch.direction == 'Down') {           
                        tthis['pull'+ touch.direction+'Move'](e);
                    }
                }
            }).on('touchend',function(e){
                 if (touch.bool) {
                    if (touch.direction == 'Up' || touch.direction == 'Down') {                 
                       tthis['pull'+ touch.direction+'End'](e);
                    }               
                 }
                 touch.bool = false;
            });      
       },

       touchDirection : function(x1, x2, y1, y2) {
            return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down');
       },

       pullMoveConditionY : function(moveNum) {
            if (moveNum < 0 ) {
                moveNum = 0;
            } else if (moveNum > $(window).height()) {
                moveNum = $(window).height();
            }
            return moveNum;
       },

       pullDownMove : function(e) {
            console.log('pullDownMove');              
           /*
             * scrollTop的值延迟响应move的值造成的，造成计算有误差坑爹的BUG，使用scrollTop进入了一个错误的误区，应该使用targetTop。targetTop才是准确的.
             */     

            var boxTop = this.opt.dom.parent().offset().top;
       
            var moveNum = Math.abs(touch.y2 - touch.y1);
          
            //计算滚动与上拉差值,注意这里有点不好理解, 方向已经带入
            var cssMove = Math.abs(touch.targetTop - (touch.y1-boxTop));

            console.log('pullDownMove',touch.y2,touch.y1,moveNum,cssMove,moveNum >= cssMove);
            
            //判断触发下拉的条件
            if (moveNum >= cssMove) {
                 touch.pull = true;
                 console.log('pullDownMove',moveNum - cssMove);
                 this.actionCss(this.pullMoveConditionY(moveNum - cssMove),0,'down'); 
            } else {
                 touch.pull = false; 
                 this.actionCss(0,0,'down');
            }

       },

       pullUpMove : function(e) {
            console.log('pullUpMove');
            var moveNum = Math.abs(touch.y2 - touch.y1),                 
                maxHeight = this.opt.dom.find('ul').height();
            //容器底部的TOP值
            var boxTop = this.opt.dom.parent().height() + this.opt.dom.parent().offset().top;
            //计算滚动与上拉差值,注意这里有点不好理解
            var cssMove = Math.abs(maxHeight - touch.targetTop - (boxTop - touch.y1));

            
            console.log('pullUpMove',touch.y2,touch.y1,moveNum,cssMove,moveNum >= cssMove);
 
            if (moveNum >= cssMove) {
                 console.log('pullUpMove',moveNum - cssMove);
                 touch.pull = true;
                 this.actionCss(this.pullMoveConditionY(moveNum - cssMove),0,'up');
            } else {
                 touch.pull = false;
                 this.actionCss(0,0,'up');
            }
      },

       pullDownEnd : function(e) {
            console.log('pullDownEnd');            
            this.actionCss(0,600,'down');
       },

       pullUpEnd : function(e) {
            console.log('pullDownEnd');             
            this.actionCss(0,600,'up');
            
       },

       actionCss : function(move,time,type) {
           if (type == 'down') {
              this.opt.dom.attr("style","transition: "+time+"ms; -webkit-transition: "+time+"ms;top:"+move/2+"px;bottom:-"+move/2+"px");
           } else  {
              this.opt.dom.attr("style","transition: "+time+"ms; -webkit-transition: "+time+"ms;top:-"+move/2+"px;bottom:"+move/2+"px");
           }
       }

       
      

    }    


    $.fn.pull = function(opt) {
         opt.dom = this;
         pull.init(opt);      
    }



})(Zepto);


